<template>
  <div class="sidebar-container">
    <div class="sidebar-header">
      <div class="user-avatar">
        <img src="../../../../public/about1.jpg">
        <div class="avatar-overlay">
          <i class="bi bi-pencil-fill"></i>
        </div>
      </div>
      <nav class="sidebar-nav">
        <button class="nav-item" @click="navigateToHome">
          <i class="bi bi-house-door"></i>
          <span>首页</span>
        </button>
        <button class="nav-item" @click="refreshPage">
          <i class="bi bi-arrow-clockwise"></i>
          <span>新搜索</span>
        </button>
      </nav>
    </div>
    <div class="history-section">
      <div class="history-header">
        <h3 class="history-title">
          <i class="bi bi-clock-history"></i>
          <span>历史搜索</span>
        </h3>
        <button class="clear-history" title="清除历史记录">
          <i class="bi bi-trash"></i>
        </button>
      </div>
      <div class="history-list">
        <slot>
          <div id="left_history_list" class="history-list-container">
            <div id="display_history" class="history-display"></div>
          </div>
        </slot>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router';
const router = useRouter();
const navigateToHome = () => {
  router.replace({ name: 'index' });
};
const refreshPage = () => {
  router.go(0);
};
</script>

<style scoped>
.sidebar-container {
  --sidebar-width: 18vw;
  --primary-color: #5a67d8;
  --hover-color: #f0f4ff;
  --border-color: #e2e8f0;
  --text-color: #2d3748;
  --light-text: #718096;
  --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  --transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);

  height: 100vh;
  width: var(--sidebar-width);
  background: linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border-color);
  overflow: hidden;
  min-width: 220px;
  max-width: 280px;
  box-shadow: var(--shadow);
  transition: var(--transition);
}

.sidebar-header {
  padding: 24px 0 16px 0;
  border-bottom: 1px solid var(--border-color);
  margin-bottom: 8px;
}

.user-avatar {
  width: 56px;
  height: 56px;
  margin: 0 auto 20px;
  border-radius: 50%;
  overflow: hidden;
  border: 3px solid white;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  position: relative;
  transition: var(--transition);
}


.user-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: var(--transition);
  color: white;
}

.user-avatar:hover .avatar-overlay {
  opacity: 1;
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 0 12px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border: none;
  background: none;
  border-radius: 8px;
  cursor: pointer;
  transition: var(--transition);
  color: var(--text-color);
  font-size: 15px;
  font-weight: 500;
  text-align: left;
  position: relative;
  overflow: hidden;
}

.nav-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 4px;
  height: 100%;
  background: #dcdee2;
  transform: scaleY(0);
  transform-origin: bottom;
  transition: var(--transition);
}

.nav-item:hover::before {
  transform: scaleY(1);
}

.nav-item:hover {
  background-color: var(--hover-color);
  color: var(--primary-color);
  transform: translateX(4px);
}

.nav-item i {
  font-size: 1.2rem;
  transition: var(--transition);
}

.history-section {
  flex: 1;
  padding: 16px 0 0 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 16px 12px;
  border-bottom: 1px solid var(--border-color);
  margin-bottom: 8px;
}

.history-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  color: var(--light-text);
  font-weight: 600;
  letter-spacing: 0.5px;
  margin: 0;
}

.clear-history {
  background: none;
  border: none;
  color: var(--light-text);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: var(--transition);
}

.clear-history:hover {
  color: #e53e3e;
  background: #fff5f5;
}

.history-list {
  flex: 1;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--primary-color) #f1f1f1;
  padding: 0 12px 12px;
}


::v-deep(.ai_history_pre_history) {
  width: 100%;
  padding: 12px 16px;
  margin: 6px 0;
  border-radius: 8px;
  background-color: white;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
  transition: var(--transition);
  font-size: 14px;
  color: var(--text-color);
  border-left: 4px solid transparent;
  position: relative;
}

::v-deep(.ai_history_pre_history:hover) {
  background-color: var(--hover-color);
  border-left: 4px solid #dcdee2;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transform: translateX(4px);
  z-index: 10;
}


::v-deep(.ai_history_pre_history::after) {
  content: '';
  position: absolute;
  bottom: 0;
  left: 16px;
  right: 16px;
  height: 1px;
  background: var(--border-color);
  transition: var(--transition);
}

::v-deep(.ai_history_pre_history:hover::after) {
  opacity: 0;
}

::v-deep(.history-item) {
  padding: 12px 16px;
  margin-bottom: 8px;
  border-radius: 8px;
  background-color: white;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: var(--transition);
  font-size: 15px;
  color: var(--text-color);
  border-left: 4px solid transparent;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-8px);}
  to { opacity: 1; transform: translateY(0);}
}

::v-deep(.ai_history_pre_history) {
  animation: fadeIn 0.35s ease forwards;
}

::v-deep(.history-item:hover) {
  background-color: var(--hover-color);
  border-left: 4px solid var(--primary-color);
  transform: translateX(4px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar-container {
    min-width: 220px;
    max-width: none;
    width: 70vw;
    position: fixed;
    z-index: 100;
    transform: translateX(-100%);
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    box-shadow: 0 0 0 rgba(0, 0, 0, 0);
  }
  
  .sidebar-container.active {
    transform: translateX(0);
    box-shadow: 20px 0 30px rgba(0, 0, 0, 0.15);
  }
  
  .user-avatar {
    width: 48px;
    height: 48px;
  }
  
  .nav-item {
    padding: 12px;
  }
}
</style>